# -*- coding:utf-8 -*-
from common.my_time import MyTime
import re
import threading
import hashlib
import time
import os
import shutil
from base import Base
from common.custom_task_data import CustomTaskData
import datetime
import platform

'''
定制任务处理
'''

class CustomTaskGetData(Base):
    def __init__(self, log_file):
        self.custom_task = CustomTaskData()
        self.url = ""
        #self.file_handler = ""
        self.news = []
        self.record_title = False
        self.zixun_cache = []
        self.task_id = 0
        
    def __del__(self):
        pass

    def recode_data(self, data_date, data_title, data_url, data_site):
        self.custom_task.add_task_detail_data(self.task_id, data_date, data_title, data_url, data_site)

    def do_search(self, sets, pre_attr, post_attr, custom_word, custom_array):
        for s in sets:
            if s == "":
                continue
            #定制词与实体相同，说明是在查询两者关系，忽略与自身关系
            if s == custom_word:
                continue
            #self.record_set_name(s)
            #self.record_title = False
            search_word = []
            result_check = [[s]]
            if pre_attr != "":
                search_word.append(pre_attr)
            search_word.append("\"" + s + "\"")
            if post_attr != "":
                search_word.append(post_attr)
            if custom_word != "":
                search_word.append(custom_word)
                result_check.append(custom_array)
            #self.get_data(search_word, result_check, ["?",";","-"])
            self.get_data_dingzhi(search_word, result_check)

    def get_begin_date(self, start_date):
        d = self.custom_task.query_task_max_date(self.task_id)
        if len(d) == 0:
            return start_date
        print(str(d[0][0] + datetime.timedelta(-7)))
        return d[0][0] + datetime.timedelta(-7)

    def generate_file(self, task_date, entity_name, start_date, custom_word, user):
        temp = str(task_date) + str(start_date) + entity_name + custom_word
        new_start_date = self.get_begin_date(start_date)
        self.set_end_date(str(new_start_date))
        self.set_max_page(20)
        self.set_check_title(False)
        entitys = self.custom_task.query_entity_info(entity_name)

        if len(entitys) == 0:
            entitys = self.custom_task.query_user_entity_info(entity_name, user)

        for entity in entitys:
            sets = entity[2].split("|")

            if custom_word == "":
                self.do_search(sets, entity[0], entity[1], "", [])
            else:
                i = 0
                custom_word_split = custom_word.split(",")
                for single_word in custom_word_split:
                    i += 1
                    #定制词过多，系统压力大
                    if i > 5:
                        break
                    self.do_search(sets, entity[0], entity[1], single_word, custom_word_split)

    def calcTaskCnt(self):
        cnt = 0
        res = os.popen("ps -ef|grep python3|grep custom_task_get_data")
        with res as f:
            for i in f.readlines():
                print(str(cnt) + ":" + i)
                cnt = cnt + 1
        return cnt
                          

    def process(self):
        task_cnt = 100
        if platform.system() == "Linux":
            task_cnt = self.calcTaskCnt()
        else:
            task_cnt = 0

        if task_cnt >= 10:
            return

        tasks = self.custom_task.query_task()
        if len(tasks) > 0:
            task = tasks[0]
            self.task_id = task[0]
            self.custom_task.update_task(task[0], 1, "")
            self.generate_file(task[1], task[2], task[3], task[4], task[5])
            self.custom_task.update_task(task[0], 5, self.url)

        if len(tasks) < 5:
            t = datetime.datetime.now()
            current_date = (t + datetime.timedelta(days=0)).strftime("%Y-%m-%d")
            old_date = (t + datetime.timedelta(days=2)).strftime("%Y-%m-%d")
            old_tasks = self.custom_task.query_old_task(old_date)
            for task in old_tasks:
                self.custom_task.update_old_task(task[0], str(task[1]), current_date)

def custom_task_get_data_api():
    task = CustomTaskGetData("./out/custom_task_get_data.log")
    task.process()

if __name__ == '__main__':
    custom_task_get_data_api()


